<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ClientManager
 *
 * @author Administrator
 */


//TODO: make some function static, or create base class. 
// private person variable; 

class ClientManager {
    //put your code here
    public function __construct(Person $p)
    {
        
        $this->_person=$p;
        $this->mysqli = new mysqli("localhost", "root", "root", "testproject");
if ($this->mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $this->mysqli->connect_errno . ") " . $this->$mysqli->connect_error;
}
//echo $this->mysqli->host_info . "\n";
        
        
    }
    
    private $_person;
    private $mysqli;
    
    public function InsertPerson()
    { 
        $person = $this->_person;
        
        $query="INSERT INTO client (
                FirstName,
                MiddleName,
                LastName,                
                DOB,
                PlaceOfBirth,
                Email,
                PhoneHome,
                PhoneCell,
                City,
                Street,
                ZIP,
                MaritalStatus,
                RegistrationDate
                ) 
                VALUE (
                '$person->FirstName',
                '$person->MiddleName',
                '$person->LastName',
                '$person->DOB',
                '$person->PlaceOfBirth',
                '$person->Email',
                '$person->PhoneHome',
                '$person->PhoneCell',
                '$person->City',
                '$person->Street',
                '$person->ZIP',
                '$person->MaritalStatus',
                NOW()
                )";
                
        $result = $this->mysqli->query($query);
        $message=$this->mysqli->error;
        $personID=mysqli_insert_id($this->mysqli); // get the latest id;
        if($result)
        $this->InsertPersonLang($personID);
        
        return $this->mysqli->error;
    }
    
    //TODO: refactor to a private person variable
    public function InsertPersonLang($personID)
    {
        
        
        $ar=  $this->_person->languagesList->Skills;
        
        foreach($ar as $key=>$value)
        {
           $message=$message . $this->InsertLanguageKnowledge($personID ,$key, $value); 
        } 
        
        return $message;
        
    }    
    
    // helper method for inserting values to many2many table
    public function InsertLanguageKnowledge($personID, $languageID,$levelID)
    {
        $query="INSERT INTO client2language (
            personID,
            LanguageID,
            Level)
            
            VALUE( 
            '$personID',
            '$languageID',
            '$levelID'
            )";
        
        $this->mysqli->query($query);
        return $this->mysqli->error;
        
    }
    
    public function LanguageIDArray() // retrieve Language=>ID collection
    {
        $query="SELECT Language, Id FROM language ORDER BY Language";
        $result=$this->mysqli->query($query);
        $Language_ID_Array;
        
        while($row=mysqli_fetch_assoc($result))
        {
            $Language_ID_Array[$row[Language]]=$row[Id]; 
        }
        
        return $Language_ID_Array;
    }
    
    public function LanguageLevelIDArray() // retrieve LanguageLevel=>ID collection ENCODING!!!!
    {
        $query="SELECT Level, Id FROM languagelevel";
        $result=$this->mysqli->query($query);
        $LanguageLevel_ID_Array;
        
        while($row=mysqli_fetch_assoc($result))
        {
            $LanguageLevel_ID_Array[$row[Level]]=$row[Id]; 
        }
        
        return $LanguageLevel_ID_Array;
    }
    
    
}


?>
